﻿using AppMVVM.Commands;
using AppMVVM.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace AppMVVM.ViewModels
{
    public class MainWindowViewModel : ViewModelBase
    {
        private UserData _user;

        ObservableCollection<ViewModelBase> _viewModels;

        private Credentials _credentials;

        private static MainWindowViewModel _instance = null;

        /// <summary>
        /// _COMMANDS
        /// </summary>
        private ICommand _connectCommand;
        private ICommand _disconnectCommand;
        private ICommand _exitCommand;

        private MainWindowViewModel()
        {
            _instance = this;
            _credentials = new Credentials();
            _user = new UserData(_credentials);
        }

        public static MainWindowViewModel Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new MainWindowViewModel();
                }
                return _instance;
            }
        }

        public string State
        {
            get
            {
                if (_user == null)
                    return ConnectionState.Disconnected.ToString();
                return _user.ConnectionStatus.ToString();
            }
        }

        public bool CanConnect
        {
            get { return _credentials.IsValid; }
        }

        public bool Connected
        {
            get { return _user != null && _user.ConnectionStatus == ConnectionState.Connected; }
        }

        public Credentials Credentials
        {
            get { return _credentials; }
        }

        public UserData UserData
        {
            get { return _user; }
        }

        public ObservableCollection<ViewModelBase> ViewModels
        {
            get
            {
                if (_viewModels == null)
                {
                    _viewModels = new ObservableCollection<ViewModelBase>();
                }
                return _viewModels;
            }
        }

        #region _COMMANDS

        public ICommand ConnectCommand
        {
            get
            {
                if (_connectCommand == null)
                {
                    _connectCommand = new DelegateCommand(Connect, delegate() { return CanConnect; });
                }

                return _connectCommand;
            }
        }

        public ICommand DisconnectCommand
        {
            get
            {
                if (_disconnectCommand == null)
                {
                    _disconnectCommand = new DelegateCommand(Disconnect, delegate() { return Connected; });
                }
                return _disconnectCommand;
            }
        }

        public ICommand ExitCommand
        {
            get
            {
                if (_exitCommand == null)
                    _exitCommand = new DelegateCommand(
                        delegate() { Application.Current.MainWindow.Close(); },
                        delegate()
                        {
                            if (Application.Current != null && Application.Current.MainWindow != null)
                                return true;
                            return false;
                        });
                return _exitCommand;
            }
        }

        #endregion

        private void Connect()
        {
            if (_user.ConnectionStatus == ConnectionState.Disconnected)
                _user.Connect();
        }

        private void Disconnect()
        {
            _user.Disconnect();
        }
    }
}
